package org.op.menu;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import org.op.FrameView;
import org.op.service.config.ConfigurationService;
import org.op.util.MessageController;

public class MenuBarViewImpl implements MenuBarView, ActionListener {
	
	private Component component;

	private MessageController messageController;
	private MenuActionController menuActionController;
	private ConfigurationService configService;
	private FrameView frameView;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2261530822900310430L;
	
	public MenuBarViewImpl() {
        
	}

	public void build() {
		JMenuBar menuBar = new JMenuBar();

		// This is a JGoodies option
        //menuBar.putClientProperty(Options.HEADER_STYLE_KEY, Boolean.TRUE);

        menuBar.add(buildFileMenu());
        menuBar.add(buildNotebookMenu());
        menuBar.add(buildEditMenu());
        menuBar.add(buildSpecialMenu());
        //menuBar.add(buildViewMenu());
        menuBar.add(buildHelpMenu());
        
        component = menuBar;
	}

	private JMenu buildNotebookMenu() {
        JMenu menu = new JMenu(messageController.getLabel("menu.notebook"));
        addMenuItem(menu, "new.notebook");
        addMenuItem(menu, "open.notebook");
        addMenuItem(menu, "archive.notebook");
		return menu;
	}

	private JMenu buildSpecialMenu() {
		JMenu menu = new JMenu(messageController.getLabel("menu.special"));
		
		addMenuItem(menu, "set.font");
		addMenuItem(menu, "set.skin");
		menu.addSeparator();
		
		addMenuItem(menu, "snip.to.new");
		addMenuItem(menu, "snip.to.archive");
		
		menu.addSeparator();
		addMenuItem(menu, "move.wikiword");
	
		menu.addSeparator();
		addMenuItem(menu, "alias.add");
		addMenuItem(menu, "alias.edit");
		
		return menu;
	}

	private JMenu buildHelpMenu() {
        JMenu menu = new JMenu(messageController.getLabel("menu.help"));
        addMenuItem(menu, "help.about");
		return menu;
	}

	private JMenu buildFileMenu() {
		
        JMenu menu = new JMenu(messageController.getLabel("menu.file"));
        addMenuItem(menu, "new.wikiword");
        addMenuItem(menu, "open.wikiword");
        addMenuItem(menu, "save.wikiword");
        addMenuItem(menu, "save.as.wikiword");
        addMenuItem(menu, "save.all");
        addMenuItem(menu, "close.wikiword");
        
        menu.addSeparator();
        
        addMenuItem(menu, "exit.app");
        
        return menu;
	}


	private JMenu buildEditMenu() {
		
        // Editor Menu
        JMenu menu = new JMenu(messageController.getLabel("menu.edit"));
        addMenuItem(menu, "edit.undo");
        addMenuItem(menu, "edit.redo");
        menu.addSeparator();
        
        addMenuItem(menu, "edit.cut");
        addMenuItem(menu, "edit.copy");
        addMenuItem(menu, "edit.paste");
        addMenuItem(menu, "edit.select.all");
        addMenuItem(menu, "edit.clear.all");
        
        menu.addSeparator();
        
        //addMenuItem(menu, "edit.find");
        // CTRL-F brings up find menu
        // TODO - make accelerators more dynamic
        JMenuItem findMenuItem = createMenuItem("edit.find");
        findMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
        menu.add(findMenuItem);
        
        addMenuItem(menu, "edit.replace");
        
        return menu;
	}
	
	
	private void addMenuItem(JMenu menu, String name) {
		//String label = name; // TODO - load this label form a resource
		JMenuItem menuitem = createMenuItem(name);
        menu.add(menuitem);		
	}
	
	private JMenuItem createMenuItem(String name) {
		String label = messageController.getLabel(name);
        JMenuItem menuitem = new JMenuItem(label);
        menuitem.addActionListener(this);
        menuitem.setName(name);
        
        return menuitem;
	}

	/**
	 *  Handles menu item selections
	 * 
	 */
	public void actionPerformed(ActionEvent event) {
		
		String menuItemName = ((JMenuItem)event.getSource()).getName();
		menuActionController.doAction(menuItemName);
		System.out.println("Clicked on " + menuItemName);
	}

	public Component getComponent() {
		return component;
	}

	public void setComponent(Component component) {
		this.component = component;
	}

	public MessageController getMessageController() {
		return messageController;
	}

	public void setMessageController(MessageController messageController) {
		this.messageController = messageController;
	}

	public MenuActionController getMenuActionController() {
		return menuActionController;
	}

	public void setMenuActionController(MenuActionController menuActionController) {
		this.menuActionController = menuActionController;
	}

	public ConfigurationService getConfigService() {
		return configService;
	}

	public void setConfigService(ConfigurationService configService) {
		this.configService = configService;
	}

	public FrameView getFrameView() {
		return frameView;
	}

	public void setFrameView(FrameView frameView) {
		this.frameView = frameView;
	}

}
